package com.kzd.gtgame.main.mine

import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.LinearLayoutManager
import com.kzd.gtgame.R
import com.kzd.gtgame.custom.setTransitionStatus
import com.kzd.gtgame.custom.setTransitionStatusE
import com.kzd.gtgame.jetpack.vm.MineViewModel
import com.kzd.gtgame.main.mine.adapter.MineMessageAdapter
import com.kzd.gtgame.main.toMessageDetailsActivity
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewModeActivity
import kotlinx.android.synthetic.main.activity_mine_message.*
import kotlinx.android.synthetic.main.include_app_refresh.*

class MineMessageActivity : OwnerViewModeActivity<MineViewModel>() {

    private val mMineMessageAdapter = MineMessageAdapter()

    override fun viewModel(): MineViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_mine_message

    override fun stateBar(): StatusBar {
        return StatusBar(color = R.color.white, view = flMineMessageBar)
    }

    override fun initView() {

        rvAppRefreshBody.apply {
            layoutManager = LinearLayoutManager(context)
        }.adapter = mMineMessageAdapter.apply {
            setOnItemClickListener { _, _, position ->
                val itemData = getItem(position)
                toMessageDetailsActivity(itemData)
            }
        }

        flMineMessageBar.setOnClickRightTextView {
            viewModel.mineMessageRead()
        }

        mSmartRefreshLayout.setOnRefreshListener {
            viewModel.mineMessage()
        }
        mSmartRefreshLayout.setOnLoadMoreListener {
            viewModel.mineMessage(isLoadMore = true)
        }
    }

    override fun initData() {
        viewModel.mineMessage()
    }

    override fun initViewMode() {

        viewModel.userMessageLiveData.observe(this as LifecycleOwner, { response ->
            setTransitionStatusE(response, mMineMessageAdapter, mSmartRefreshLayout)
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            val isLoadMore = response.data?.isLoadMore
                ?: return@observe
            if (isLoadMore) {
                mMineMessageAdapter.addData(data)
            } else {
                mMineMessageAdapter.setNewInstance(data)
                viewModel.mineMessageRead()
            }
        })

        viewModel.mineMessageReadLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status != Status.SUCCESS)
                return@observe
            viewModel.userAppTips()
        })
    }
}